package com.zzk.lanouhn.myapplication1.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;

import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.zzk.lanouhn.myapplication1.R;
import com.zzk.lanouhn.myapplication1.adapters.ShowHotDetailsAdapter;
import com.zzk.lanouhn.myapplication1.modles.HotMyGridView;
import com.zzk.lanouhn.myapplication1.utils.HttpUtils;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * 热门推荐详情
 * Created by lanouhn on 16/9/8.
 */
public class HotDetailsActivity extends AppCompatActivity {
    private XRecyclerView xRecyclerView;
    private ImageView iv_back;
    private List<HotMyGridView.DataBean> hotMyGridView = new ArrayList<>();
    private List<HotMyGridView.DataBean> tempHotGridView = new ArrayList<>();
    private ShowHotDetailsAdapter adapter;
    int page = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_showhotdetails);
        initView();
        getDataFromWeb();
        initEvent();
    }


    private void initView() {
        xRecyclerView = (XRecyclerView) findViewById(R.id.recy_show_hot_details);
        iv_back= (ImageView) findViewById(R.id.iv_back_show_hot);
        LinearLayoutManager manager = new LinearLayoutManager(HotDetailsActivity.this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        xRecyclerView.setLayoutManager(manager);
        //上拉加载的动画效果
        xRecyclerView.setRefreshProgressStyle(ProgressStyle.BallBeat);
//        //下拉刷新的动画效果
        xRecyclerView.setArrowImageView(R.drawable.ic_loading_rotate);
        xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page=0;
                getDataFromWeb();
            }

            @Override
            public void onLoadMore() {
                page += 1;
                getDataFromWeb();
            }
        });
    }
    private void initEvent(){
         Bundle bundle=this.getIntent().getExtras();
        int position=bundle.getInt("position");
        xRecyclerView.scrollToPosition(position+1);

        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void getDataFromWeb() {
        String s = String.valueOf(page);
        String url = "http://open4.bantangapp.com/community/post/hotRecommend?";
        String dataUrl = "app_id=com.jzyd.BanTang&client_id=bt_app_android" +
                "&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d" +
                "&track_user_id=&oauth_token=&track_deviceid=863121028526879" +
                "&track_device_info=MI+2S&channel_name=xiaomi&app_installtime=1473128006" +
                "&app_versions=5.8.7&os_versions=5.0.2&screensize=720&v=19" +
                "&page=" + s + "&pagesize=18";
        try {
            HttpUtils.doPostAsyn(url, dataUrl, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson = new Gson();
                    tempHotGridView = gson.fromJson(result, HotMyGridView.class).getData();
                    if (tempHotGridView.size() > 0) {
                        handler.sendEmptyMessage(1);
                    } else {
                        handler.sendEmptyMessage(0);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:

                    break;
                case 1:
                    if (page == 0) {
                        hotMyGridView.clear();
                        hotMyGridView.addAll(tempHotGridView);
                        adapter = new ShowHotDetailsAdapter(HotDetailsActivity.this, hotMyGridView);
                        xRecyclerView.setAdapter(adapter);
                        xRecyclerView.refreshComplete();
                        tempHotGridView.clear();
                    } else {
                        hotMyGridView.addAll(tempHotGridView);
                        adapter.notifyDataSetChanged();
                        xRecyclerView.loadMoreComplete();
                        tempHotGridView.clear();
                    }

                    break;
                default:

                    break;
            }
        }
    };

}
